package com.atguigu.srb.core.controller.api;

import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.WxLoginService;
import com.atguigu.srb.result.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * ClassName: ApiWxLoginController
 * Package: com.atguigu.srb.core.controller.api
 * Description:
 *
 * @Author 刘强
 * @Create 2024/4/21 14:44
 * @Version 1.0
 */
@Api(tags = "微信登陆接口")
@Controller
@RequestMapping("/api/wx")
public class ApiWxLoginController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private WxLoginService wxLoginService;
    /*
    # 微信开放平台 appid
    appId: wxed9954c01bb89b47
    # 微信开放平台 appsecret
    appSecret: a7482517235173ddb4083788de60b90e
    # 微信开放平台 重定向url(localhost:8160 必须和wx平台配置的授权回调域一致)
    redirectUri: http://localhost:8160/api/ucenter/wx/callback
 */
    @ApiOperation("微信登陆，获取验证码接口")
    @GetMapping("/login")
    public String login(HttpSession session){
        return wxLoginService.login(session);

    }
    // 用户授权后，我们应该直接  响应token 并且跳转到首页  ---> redirect + 查询字符串的方式重定向
    @ApiOperation("微信扫码登陆成功后重定向到的接口")
    @GetMapping("/callback")
    public String  callBack(@RequestParam String code, @RequestParam String state, HttpSession session, HttpServletRequest request) throws Exception {
        return wxLoginService.callBack(code,state,session,request);
    }

    /**
     *  测试会话共享问题
     */
    @GetMapping("/set")
    @ResponseBody
    public ResponseVo setSession(HttpSession session){
        session.setAttribute("token", UUID.randomUUID().toString().replace("-",""));
        System.out.println("sessionId = " + session.getId());
        return ResponseVo.ok().addData("sessionId" ,session.getId());
    }
    @GetMapping("/get")
    @ResponseBody
    public ResponseVo getSession(HttpSession session){
       Object  token = session.getAttribute("token");
        System.out.println("sessionId = " + session.getId());
        System.out.println("token = " + token);
        return ResponseVo.ok();
    }
}
